﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

namespace publicLibrary
{
    public class imgVerifyCode
    {

        //验证码位数
        public int VCODENUM = 4;

        public string VCODE
        {
            get;
            private set;
        }

        //验证码字体大小
        private const int fontSize = 16;

        //字体
        private string fontFamily = "宋体,黑体,隶书,幼圆";

        //定义验证码字符
        private const string VCHAR = "丰王井开夫天无元专云扎艺木五支厅不太犬区历尤友匹车巨牙屯比互切瓦止少日中冈贝内水见午牛手毛气升长仁什片仆化仇币仍仅斤爪反介父从今凶分乏公仓月氏勿欠风丹匀乌凤勾文六方火为斗忆订计户认心尺引丑巴孔队办以允予劝双书幻玉刊示末未击打巧正扑扒功扔去甘世古节本术可丙左厉右石布龙平灭轧东卡北占业旧帅归且旦目叶甲申叮电号田由史只央兄叼叫另叨叹四生失禾丘付仗代仙们仪白仔他斥瓜乎丛令用甩印乐句匆册犯外处冬鸟务包饥主市立闪兰半汁汇头汉宁穴它讨写让礼训必议讯记永司尼民出辽奶奴加召皮边发孕圣对台矛纠母幼丝";


        private Random rnd = new Random();


        public MemoryStream GetByValidateCode()
        {
            Bitmap Img = null;
            Graphics g = null;
            MemoryStream ms = null;

            VCODE = rndChar(VCODENUM);

            int gheight = VCODE.Length * 32;

            Img = new Bitmap(gheight, 24);

            g = Graphics.FromImage(Img);

            g.Clear(rndColor(204, 255));//设定背景色
            Pen blackPen = new Pen(Color.Ivory, 3);

            for (int i = 0; i < 155; i++)// 随机产生干扰线，使图象中的认证码不易被其它程序探测到
            {
                int x = rnd.Next(gheight);
                int y = rnd.Next(20);
                int xl = rnd.Next(6);
                int yl = rnd.Next(2);
                g.DrawLine(blackPen, x, y, x + xl, y + yl);
            }

            SolidBrush s = new SolidBrush(Color.SandyBrown);

            int ii = 0;
            foreach (var item in VCODE)
            {
                g.DrawString(item.ToString(), rndFont(), s, 3 + ii + rnd.Next(-5, 5), 3);
                ii += 23;
            }
            
            
            ms = new MemoryStream();
            Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            g.Dispose();
            Img.Dispose();
            return ms;
        }


        private Color rndColor(int fc, int bc)
        {

            if (fc > 255) fc = 255;
            if (bc > 255) bc = 255;
            //if(ac>255) ac=255;
            int r = fc + rnd.Next(bc - fc);
            int g = fc + rnd.Next(bc - fc);
            int b = fc + rnd.Next(bc - bc);
            Color rs = Color.FromArgb(r, g, b);
            return rs;
        }



        /// <summary>
        /// 设定返回随机字符串的位数
        /// </summary>
        /// <returns></returns>
        private string rndChar(int len)
        {
            char[] cahrArr = VCHAR.ToCharArray();
            int cahrArrLen = cahrArr.Length;
            string re = "";

            for (int i = 0; i < len; i++)
            {
                re += cahrArr[rnd.Next(0, cahrArrLen)];
            }
            return re;
        }

        private Font rndFont()
        {
            string[] fontFamilyArr = fontFamily.Split(',');
            return new Font(fontFamilyArr[rnd.Next(0, fontFamilyArr.Length)], fontSize);
        }


    }
}